package com.tdy.nht.doc.api.impl;

import com.github.pagehelper.PageInfo;
import com.tdy.nht.common.Result;
import com.tdy.nht.common.ResultPage;
import com.tdy.nht.constant.StatusCodeConstants;
import com.tdy.nht.doc.domain.${modelName}DO;
import com.tdy.nht.doc.domain.${modelName}DTO;
import com.tdy.nht.doc.domain.${modelName}Query;
import com.tdy.nht.doc.service.${modelName}Service;
import com.tdy.nht.util.BeanCopierUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
* @Description $!{annotation}API组件}
* @author $!{author}
* @Date ${createTime}
*/
@RestController
public class ${modelName}ApiImpl {
    private static final Logger logger = LoggerFactory.getLogger(${modelName}ApiImpl.class);
    @Autowired
    private ${modelName}Service ${lowerModelName}Service;

    /**
    * 查询$!{annotation}列表
    *
    * @param query 查询条件
    * @return 返回响应结果
    */
    @PostMapping(value = "/list${modelName}Page")
    public ResultPage<${modelName}DTO> list${modelName}Page(@RequestBody ${modelName}Query query) {
        ResultPage<${modelName}DTO> resultPage = new ResultPage<>();
        List<${modelName}DTO> list = new ArrayList<>();
            try {
                    PageInfo<${modelName}DO> pageInfo = ${lowerModelName}Service.listByPage(query);
                    long total = pageInfo.getTotal();
                    if (total > 0) {
                        BeanCopierUtils.cloneList(pageInfo.getList(), list, ${modelName}DTO.class);
                        resultPage.setData(list);
                    }else{
                        resultPage.setData(list);
                    }
                        resultPage.setDraw(query.getDraw());
                        resultPage.setRecordsFiltered(total);
                        resultPage.setRecordsTotal(total);
                        return resultPage;
                } catch (Exception e) {
                    logger.error("查询$!{annotation}列表错误", e);
                    resultPage.setCode(StatusCodeConstants.ERROR);
                    return resultPage;
                }
    }

                /**
                * @Description: 根据id查询
                * @param: id 查询详情的id
                * @return: 响应结果
                */
                @GetMapping("/get${modelName}ById")
                public Result<${modelName}DTO> get${modelName}ById(@RequestParam("id") Long id) {
                    Result<${modelName}DTO> result = new Result<>();
                        try {
                        ${modelName}DO ${lowerModelName}DO = ${lowerModelName}Service.getDetail(id);
                        if (${lowerModelName}DO != null) {

                        ${modelName}DTO ${lowerModelName}DTO = BeanCopierUtils.copyProperties(${lowerModelName}DO, ${modelName}DTO.class);
                        result.setData(${lowerModelName}DTO);
                        }
                        return result;
                        } catch (Exception e) {
                        logger.error("查询$!{annotation}", e);
                        result.setCode(StatusCodeConstants.ERROR);
                        return result;
                        }
                        }


                        /**
                        * 批量删除$!{annotation}
                        * @param ids 记录id
                        * @return 响应结果
                        */
                        @PostMapping("/remove${modelName}Batch")
                        public Result<Object> remove${modelName}Batch(@RequestBody List<Long> ids) {
                                Result<Object> result = new Result<>();
                                int num = ${lowerModelName}Service.deleteBatch${modelName}(ids);
                                return result;
                        }


#if($!saveMethod == '1')
    /**
      * 保存$!{annotation}
      *
      * @param ${lowerModelName}DTO $!{annotation}实例
      * @return 响应结果
      */
    @PostMapping("/save${modelName}")
    public Result<Object> save${modelName}(@RequestBody ${modelName}DTO ${lowerModelName}DTO) {
            Result<Object> result = new Result<>();
            try {
                 ${modelName}DO ${lowerModelName}DO = BeanCopierUtils.copyProperties(${lowerModelName}DTO, ${modelName}DO.class);
				Long id = ${lowerModelName}DO.getId();
				if (id != null) {
				//更新
                ${modelName}DO detail = ${lowerModelName}Service.getDetail(id);
				if (detail == null) {
                    logger.warn("通过{0}查询$!{annotation}没有查询到数据", id);
                    result.setCode(StatusCodeConstants.ERROR);
                    return result;
                }
                ${lowerModelName}DO.setModifyTime(new Date());
                ${lowerModelName}DO.setCreateTime(detail.getCreateTime());
                ${lowerModelName}Service.updateSelectiveByPrimary(${lowerModelName}DO);
				return result;
            } else {
                ${lowerModelName}DO.setCreateTime(new Date());
                ${lowerModelName}Service.save${modelName}(${lowerModelName}DO);
				return result;
            }
            } catch (Exception e) {
				logger.error("保存设备类型报错", e);
				result.setCode(StatusCodeConstants.ERROR);
				return result;
            }
        }
#end
}
